"use strict";
const common_vendor = require("../../common/vendor.js");
const utils_request_status = require("./status.js");
const utils_auth_index = require("../auth/index.js");
const utils_storage_index = require("../storage/index.js");
const store_modules_user_index = require("../../store/modules/user/index.js");
let isRefreshing = false;
let requestQueue = [];
function requestInterceptors() {
  common_vendor.index.$u.http.interceptors.request.use(
    (config) => {
      var _a, _b;
      config.data = config.data || {};
      const isToken = ((_a = config.custom) == null ? void 0 : _a.auth) === false;
      const isRepeatSubmit = ((_b = config.custom) == null ? void 0 : _b.repeatSubmit) === false;
      if (utils_auth_index.getToken() && !isToken && config.header) {
        config.header.token = utils_auth_index.getToken();
      }
      if (!isRepeatSubmit && (config.method === "POST" || config.method === "UPLOAD")) {
        const requestObj = {
          url: config.url,
          data: typeof config.data === "object" ? JSON.stringify(config.data) : config.data,
          time: (/* @__PURE__ */ new Date()).getTime()
        };
        const sessionObj = utils_storage_index.storage.getJSON("sessionObj");
        if (!sessionObj) {
          utils_storage_index.storage.setJSON("sessionObj", requestObj);
        } else {
          const s_url = sessionObj.url;
          const s_data = sessionObj.data;
          const s_time = sessionObj.time;
          const interval = 1e3;
          if (s_data === requestObj.data && requestObj.time - s_time < interval && s_url === requestObj.url) {
            const message = "数据正在处理，请勿重复提交";
            console.warn(`[${s_url}]: ${message}`);
            return Promise.reject(new Error(message));
          } else {
            utils_storage_index.storage.setJSON("sessionObj", requestObj);
          }
        }
      }
      return config;
    },
    (config) => Promise.reject(config)
  );
}
function responseInterceptors() {
  common_vendor.index.$u.http.interceptors.response.use(
    async (response) => {
      const data = response.data;
      const config = response.config;
      const custom = config == null ? void 0 : config.custom;
      if (data.code === 200)
        return data || {};
      if (data.code === 401) {
        if (!isRefreshing) {
          isRefreshing = true;
          await store_modules_user_index.useUserStore().authLogin();
          requestQueue.forEach((cb) => cb());
          requestQueue = [];
          isRefreshing = false;
          return common_vendor.index.$u.http.request(config);
        } else {
          return new Promise((resolve) => {
            requestQueue.push(() => {
              resolve(common_vendor.index.$u.http.request(config));
            });
          });
        }
      }
      if ((custom == null ? void 0 : custom.toast) !== false)
        common_vendor.index.$u.toast(data.message);
      if (custom == null ? void 0 : custom.catch) {
        return Promise.reject(data);
      } else {
        return new Promise(() => {
        });
      }
    },
    (response) => {
      if (response.statusCode) {
        utils_request_status.showMessage(response.statusCode);
        return Promise.reject(response.data);
      }
      utils_request_status.showMessage("网络连接异常,请稍后再试!");
    }
  );
}
exports.requestInterceptors = requestInterceptors;
exports.responseInterceptors = responseInterceptors;
